<div class="layuimini-main">

    <div class="layui-form layuimini-form" lay-filter="main">
        <div class="layui-form-item">
            <label class="layui-form-label required">书号</label>
            <div class="layui-input-block">
                <input type="text" name="ID" lay-verify="required" lay-reqtext="书号不能为空" placeholder="请输入书号" value="" disabled class="layui-input layui-disabled">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">书名</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" lay-reqtext="书名不能为空" placeholder="请输入书名" value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">借出数</label>
            <div class="layui-input-block">
                <input type="number" name="borrowed_num" lay-verify="required" lay-reqtext="借出数不能为空" placeholder="请输入该书籍的总数" value="" disabled class="layui-input layui-disabled">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">库存总数</label>
            <div class="layui-input-block">
                <input type="number" name="total_num" lay-verify="required" lay-reqtext="库存总数不能为空" placeholder="请输入该书籍的总数" value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">出版社</label>
            <div class="layui-input-block">
                <input type="text" name="publisher" placeholder="请输入出版社名" value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'table'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            $ = layui.$;

        form.val("main", {
            "ID":window.sessionStorage.getItem('book.ID'),
            "name":window.sessionStorage.getItem('book.name'),
            "publisher":window.sessionStorage.getItem('book.publisher'),
            "borrowed_num":window.sessionStorage.getItem('book.borrowed_num'),
        });

        if(window.sessionStorage.getItem('book.total_num')){
            form.val("main", {
                "total_num":window.sessionStorage.getItem('book.total_num')
            });
        }


        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;

        //监听提交
        form.on('submit(saveBtn)', function (data) {

            $.ajax({
                url:'/index/admin/editBookJson'
                ,type: 'POST'
                ,data: data.field
                ,success:function (res) {
                    if(res.code == "1"){
                        var index = layer.msg(res.message, {
                            icon: 1
                        }, function () {

                            // 关闭弹出层
                            layer.close(index);
                            layer.close(parentIndex);
                            window.parent.location.reload();
                        });
                    }else{
                        layer.msg(res.message, {icon: 2});
                    }
                }
            });

            return false;
        });

    });
</script>